"""
================================================================================
                     ------------utf-8--------------
================================================================================
@Author: rfdsg
@Create Time: 2023/11/3 - 21:09
@Description:
@Attention:
"""
import pickle

# 读取nc文件
import xarray as xr
path = r'mss_sio_32.1_WGS84.nc'
dataset=xr.open_dataset(path)

# 获取数据集结构
# df = dataset.to_dataframe()
# print(df)
# print(dataset.z)
# print(dataset.coords)
# print(dataset)

# 获取经纬度变量
lon = dataset.variables['lon'].values
lat = dataset.variables['lat'].values

# 定义经纬度范围
lon_min, lon_max = 114.0900, 117.0900  # 经度范围
lat_min, lat_max = 8.0900, 11.0900   # 纬度范围

# 筛选满足经纬度范围内的数据
lon_range = slice(lon_min, lon_max)
lat_range = slice(lat_min, lat_max)
dataset = dataset.sel(lon = lon_range, lat =lat_range).to_dataframe()
dataset.reset_index(inplace=True)

# 存储打印数据
# with open('mss_sio_32.1_train.pickle', 'wb') as f:
#     pickle.dump(dataset, f)
print(dataset.head())